
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class Register extends Form implements CommandListener, Runnable {

    private TextField txtUsername;
    private TextField txtPassword;
    private TextField txtRePassword;
    private Command cmdRegister;
    private Command cmdBack;
    private UpAnh upAnh;

    public Register(UpAnh upAnh) {
        super("Dang ky");
        this.upAnh = upAnh;
        txtUsername = new TextField("Tai khoan", null, 30, TextField.ANY);
        txtPassword = new TextField("Mat khau", null, 30, TextField.PASSWORD);
        txtRePassword = new TextField("Go lai mat khau", null, 30, TextField.PASSWORD);
        this.append(txtUsername);
        this.append(txtPassword);
        this.append(txtRePassword);
        cmdRegister = new Command("Dang ky", Command.OK, 2);
        cmdBack = new Command("Back", Command.BACK, 2);
        this.addCommand(cmdRegister);
        this.addCommand(cmdBack);
        this.setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdRegister) {
            if (!Check()) {
                upAnh.client.Register(txtUsername.getString(), txtPassword.getString());
//                new Thread(this).start();
                upAnh.DisplayLogin();
            }
        } else if (c == cmdBack) {
            upAnh.DisplayLogin();
        }
    }

    private boolean Check() {
        boolean isError = false;
        if (txtUsername.getString().equals("")) {
            this.append("Ban chua nhap tai khoan");
            isError = true;
        }
        if (txtPassword.getString().equals("")) {
            this.append("Ban chua nhap mat khau");
            isError = true;
        }
        if (!txtPassword.getString().equals(txtRePassword.getString())) {
            this.append("Hai mat khau khong giong nhau");
            isError = true;
        }
        return isError;
    }

    public void run() {
        while (true) {
            if (upAnh.client.register != null) {
                if (upAnh.client.register.equals("false")) {
                    this.append("Dang nhap khong thanh cong");
                } else {
                    upAnh.DisplayLogin();
                }
                return;
            }
        }
    }
}
